package com.smart.manager.system.handler;


import cn.hutool.core.collection.CollUtil;
import com.smart.manager.system.entities.models.SystemScheduleTask;
import com.smart.manager.system.repository.ScheduleTaskRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 */
@Log4j2
@Service
@RequiredArgsConstructor
public class TaskLoadHandler implements CommandLineRunner {

    private final ScheduleTaskRepository scheduleTaskRepository ;
    private final TaskRegHandler taskRegHandler;


    /**
     * 程序启动时间加载定时任务
     */
    @Override
    public void run(String... args) {
        log.info("正在加载系统任务......");
        List<SystemScheduleTask> taskList = scheduleTaskRepository.getRunTaskList();
        if (CollUtil.isNotEmpty(taskList)) {
            for (SystemScheduleTask item : taskList) {
                taskRegHandler.addTask(new TaskRunHandler(item), item.getCron());
                log.info("定时任务:{} 加载成功", item.getTitle());
            }
        }
        log.info("定时任务加载完成......");
    }
}